package com.sheyu.mall.product.app.category;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sheyu.mall.product.entity.category.CategoryEntity;
import com.sheyu.mall.product.service.category.CategoryService;
import com.sheyu.common.utils.R;

import javax.validation.Valid;


/**
 * 商品三级分类
 *
 * @author sheyu
 * @email sheyu@XXX.com
 * @date 2020-07-27 13:43:00
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有列表，包括子列表
     */
    @GetMapping("/list/tree")
    //@RequiresPermissions("product:category:list")
    public R list(){

        List<CategoryEntity> categoryEntityList = categoryService.getAllCategories();
        return R.ok().put("data", categoryEntityList);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody @Valid CategoryEntity category){
		categoryService.save(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateCascade(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
        //检查当前的菜单是否其他引用
        categoryService.removeCategoryByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
